
# 第四次更改
from rest_framework import serializers
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model

User = get_user_model() #使用get_user_model()获取自定义用户模型

class UserRegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'password']
        extra_kwargs = {
            'password': {'write_only': True}
        }

    def create(self, validated_data):
        user = User(
            username=validated_data['username']
        )
        user.set_password(validated_data['password'])
        user.save()
        return user

class AdminRegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'password','email', 'is_staff']
        extra_kwargs = {
            'password': {'write_only': True}
        }

    def create(self, validated_data):
        admin_user = User(
            username=validated_data['username'],
            # phone_number=validated_data['phone_number'],
            email=validated_data['email'],
            is_staff=True
        )
        admin_user.set_password(validated_data['password'])
        admin_user.save()
        return admin_user

# 第三次更改：登陆注册可以但是管理员用户不能注册

# class UserRegisterSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         fields = ['username', 'password']

#     def create(self, validated_data):
#         user = User(
#             username=validated_data['username']
#         )
#         user.set_password(validated_data['password'])
#         user.save()
#         return user

# class AdminRegisterSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         fields = ['username', 'password', 'phone_number', 'email', 'is_admin']

#     def create(self, validated_data):
#         admin_user = User(
#             username=validated_data['username'],
#             phone_number=validated_data['phone_number'],
#             email=validated_data['email'],

#             is_admin=True
#         )
#         admin_user.set_password(validated_data['password'])
#         admin_user.save()
#         return admin_user

class LoginSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField()

# 实现类似仪表盘的页面展示，包含图表、统计数据等内容
# 使用 Django REST Framework 为你的模型创建序列化器，以便将数据转换为 JSON 格式
from rest_framework import serializers
from .models import DashboardData

class DashboardDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = DashboardData
        fields = '__all__'
